#  Copyright 2008-2009 NVIDIA Corporation
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License

#
#
# OpenCurrent project root CMakeLists.txt
#
#

cmake_minimum_required(VERSION 2.6.4)

project(opencurrent)

ENABLE_TESTING()

# Turn off chatty makefiles by default.  To make them chatty again,
# call make with VERBOSE=1 like this:
#
# make VERBOSE=1

SET(CMAKE_VERBOSE_MAKEFILE OFF)

SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake/cmake_modules/")
INCLUDE(${CMAKE_SOURCE_DIR}/System.cmake)
INCLUDE(${CMAKE_SOURCE_DIR}/CMake/cuda/FindCUDA.cmake)

# Clear default flags, they are bad
SET(CMAKE_CXX_FLAGS "")
SET(CMAKE_C_FLAGS "")

IF (OCU_OMP_ENABLED)
  FIND_PACKAGE(OpenMP)

  if (OPENMP_FOUND)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
  else(OPENMP_FOUND)
    MESSAGE( FATAL_ERROR "Compiler must support OpenMP")  
  endif(OPENMP_FOUND)

  ADD_DEFINITIONS(-DOCU_OMP)
ENDIF (OCU_OMP_ENABLED)

IF (NOT DEFINED OCU_TARGET_SM)
  SET(OCU_TARGET_SM "sm_13")
ENDIF (NOT DEFINED OCU_TARGET_SM)

if (OCU_TARGET_SM STREQUAL "sm_10")
  SET(DOUBLE_SUPPORT_ENABLED FALSE)
  SET(ATOMIC_SUPPORT_ENABLED FALSE)
  ADD_DEFINITIONS(-DOCU_SM10)
elseif (OCU_TARGET_SM STREQUAL "sm_11")
  SET(DOUBLE_SUPPORT_ENABLED FALSE)
  SET(ATOMIC_SUPPORT_ENABLED TRUE)
  ADD_DEFINITIONS(-DOCU_SM11)
elseif (OCU_TARGET_SM STREQUAL "sm_12")
  SET(DOUBLE_SUPPORT_ENABLED FALSE)
  SET(ATOMIC_SUPPORT_ENABLED TRUE)
  ADD_DEFINITIONS(-DOCU_SM12)
elseif (OCU_TARGET_SM STREQUAL "sm_13")
  SET(DOUBLE_SUPPORT_ENABLED TRUE)
  SET(ATOMIC_SUPPORT_ENABLED TRUE)
  ADD_DEFINITIONS(-DOCU_SM13)
elseif (OCU_TARGET_SM STREQUAL "sm_20")
  SET(DOUBLE_SUPPORT_ENABLED TRUE)
  SET(ATOMIC_SUPPORT_ENABLED TRUE)
  ADD_DEFINITIONS(-DOCU_SM20)
else()
  MESSAGE( FATAL_ERROR "Unknown OCU_TARGET_SM: '${OCU_TARGET_SM}', must be one of: 'sm_10' 'sm_11' 'sm_12' 'sm_13' 'sm_20'")
endif()

SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-arch;${OCU_TARGET_SM}")

IF (DOUBLE_SUPPORT_ENABLED)
  ADD_DEFINITIONS(-DOCU_DOUBLESUPPORT)
ELSE (DOUBLE_SUPPORT_ENABLED)
  ADD_DEFINITIONS(-DOCU_NODOUBLESUPPORT)
ENDIF (DOUBLE_SUPPORT_ENABLED)

IF (ATOMIC_SUPPORT_ENABLED)
  ADD_DEFINITIONS(-DOCU_ATOMICSUPPORT)
ELSE (ATOMIC_SUPPORT_ENABLED)
  ADD_DEFINITIONS(-DOCU_NOATOMICSUPPORT)
ENDIF (ATOMIC_SUPPORT_ENABLED)

IF (OCU_NETCDF_ENABLED)
  ADD_DEFINITIONS(-DOCU_NETCDF)
  IF (OCU_NETCDF4_SUPPORT_ENABLED)
    ADD_DEFINITIONS(-DOCU_NETCDF4SUPPORT)
  ENDIF (OCU_NETCDF4_SUPPORT_ENABLED)
ENDIF (OCU_NETCDF_ENABLED)


INCLUDE_DIRECTORIES(
  ${CMAKE_SOURCE_DIR}
  ${CUDA_INSTALL_PREFIX}/include
  )

CUDA_INCLUDE_DIRECTORIES(
  ${CMAKE_SOURCE_DIR}
  ${CUDA_INSTALL_PREFIX}/include
  )

IF (OCU_NETCDF_ENABLED)
  INCLUDE_DIRECTORIES(${NetCDF_INCLUDE_DIR})
  CUDA_INCLUDE_DIRECTORIES(${NetCDF_INCLUDE_DIR})
ENDIF (OCU_NETCDF_ENABLED)


#windows specific settings for C
IF(CMAKE_C_COMPILER MATCHES cl)
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
  ADD_DEFINITIONS(-DNOMINMAX)
ENDIF(CMAKE_C_COMPILER MATCHES cl)

#windows specific settings for C++
IF(CMAKE_CXX_COMPILER MATCHES cl)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996 /wd4231 /EHsc")
  ADD_DEFINITIONS(-DNOMINMAX)
ENDIF(CMAKE_CXX_COMPILER MATCHES cl)


ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/ocuutil)
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/ocustorage)
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/ocuequation)
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/apps)
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/tests)

